/**
 * Project Name:cnsy-url-short-service
 * File Name:FileSizeUtil.java
 * Package Name:cn.cnsy123.shorturls.business.util 
 * Date: 2020年4月6日-下午11:27:00
 * Copyright (c) 2020, yangpengfei@360@126.com All Rights Reserved. 
 *
 */

package cn.cnsy123.shorturls.business.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * ClassName: FileSizeUtil
 * 
 * @author yangpengfei
 * @date 2020年4月6日-下午11:27:00
 * @version 1.0
 * @since JDK 1.7
 */
public class FileSizeUtil {
    
    
    private static final long ONE_KB = 1024L;
    
    public static final long ONE_MB = ONE_KB * ONE_KB;
    
    public static final long ONE_GB = ONE_KB * ONE_MB;
    
    public static final long ONE_TB = ONE_KB * ONE_GB;
    
    public static final long ONE_PB = ONE_KB * ONE_TB;
    
    public static final long ONE_EB = ONE_KB * ONE_PB;
    
    public static String convertCustomFileSize(long fileSize) {
        if(fileSize >= ONE_GB) {
            return String.format("%.1f GB", (float) fileSize / ONE_GB);
        } else if (fileSize >= ONE_MB) {
            float f = (float) fileSize / ONE_MB;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (fileSize >= ONE_KB) {
            float f = (float) fileSize / ONE_KB;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } 
        
        return String.format("%d B",fileSize);
    }
    
    
    public static String fileUploadProgress(long byteRead,long contentlength) {
        BigDecimal bigDecimal = (BigDecimal.valueOf(byteRead).divide(BigDecimal.valueOf(contentlength),2,RoundingMode.HALF_UP));
        return bigDecimal.multiply(BigDecimal.valueOf(100L)).toString()+"%";
    }

}
